﻿using System;
using System.Collections.Generic;
namespace Common
{
	public class RouteData<T> where T : class
	{
		public RouteData()
		{
		}

		private List<KeyValuePair<string, T>> _route_list = new List<KeyValuePair<string, T>>();
		private Dictionary<Dictionary<string, string>, T> _route_value_list = new Dictionary<Dictionary<string, string>, T>();
		private List<string> _route_format_list = new List<string>();

		/// <summary>
		/// 从路由数据中查找对应数据
		/// </summary>
		/// <returns>The value.</returns>
		/// <param name="key">Key.</param>
		public T FindValue(string key)
		{
			foreach (var item in _route_list)
			{
				if (item.Key == key)
				{
					return item.Value;
				}
			}

			return null;
		}

		/// <summary>
		/// 从路由数据中查找对应数据
		/// </summary>
		/// <returns>The value.</returns>
		/// <param name="keys">Keys.</param>
		public object FindValue(IEnumerable<string> keys) 
		{
			foreach (var item in keys)
			{
				var obj = FindValue(item);
				if (obj != null)
					return obj;
			}

			return null;
		}

		/// <summary>
		/// 注册路由列表
		/// </summary>
		/// <param name="key">Key.</param>
		/// <param name="obj">Object.</param>
		public void RegisterRouteData(string key, T obj)
		{
			_route_list.Add(new KeyValuePair<string, T>(key, obj));
		}

		/// <summary>
		/// 获取当前的路由列表
		/// </summary>
		/// <returns>The route list.</returns>
		public List<KeyValuePair<string, T>> GetRouteList()
		{
			return _route_list;
		}

		/// <summary>
		/// 注册路由格式化数据
		/// </summary>
		/// <param name="route">Route.</param>
		public void RegisterRouteFormat(string route)
		{
			if (!string.IsNullOrEmpty(route))
			{
				_route_format_list.Add(route);
			}
		}

		/// <summary>
		/// 注册路由列表数据
		/// </summary>
		/// <param name="routevalue">Routevalue.</param>
		/// <param name="obj">Object.</param>
		/// <param name="reset_list">If set to <c>true</c> reset list.</param>
		public void RegisterRouteData(Dictionary<string, string> routevalue, T obj, bool reset_list = true)
		{
			_route_value_list.Add(routevalue, obj);

			if (reset_list)
				RestRouteList();
		}

		/// <summary>
		/// 重新计算路由列表
		/// </summary>
		public void RestRouteList()
		{
			_route_list.Clear();

			foreach (var item in _route_format_list)
			{
				foreach (var route in _route_value_list)
				{
					var key = item;
					foreach (var kv in route.Key)
					{
						key = key.Replace("{" + kv.Key + "}", kv.Value);
					}

					if (key.IndexOf('{') == -1 && key.IndexOf('}') == -1)
					{
						RegisterRouteData(key, route.Value);
					}
				}
			}

		}

		/// <summary>
		/// 根据变换列表和注册的格式获取路由名称列表
		/// </summary>
		/// <returns>The route key.</returns>
		/// <param name="routevalue">Routevalue.</param>
		public List<string> GetRouteKey(Dictionary<string, string> routevalue)
		{
			List<string> list = new List<string>();

			foreach (var item in _route_format_list)
			{
				var key = item;
				foreach (var kv in routevalue)
				{
					key = key.Replace("{" + kv.Key + "}", kv.Value);
				}

				if (key.IndexOf('{') == -1 && key.IndexOf('}') == -1)
				{
					list.Add(key);
				}
			}

			return list;
		}
	}
}
